/*
 * 版权所有 (C) 2018 知启蒙(ZHIQIM) 保留所有权利。
 * 
 * 欢迎到知启蒙网站（https://www.zhiqim.com）购买正版软件，知启蒙还提供许多开源框架和软件。
 * 
 * 1、本软件产品所有源代码受《中华人民共和国著作权法》和其他有关法律、法规的保护，其所有知识产权归湖南知启蒙科技有限公司所有；
 * 2、禁止复制和修改。不得复制修改、翻译或改编本软件所有源代码，或者基于本软件产品创作衍生作品；
 * 3、禁止进行逆向工程。不得对本软件的源代码进行逆向工程、反编译或试图以其他方式发现软件的源代码；
 * 4、个别授权：如需进行商业性的销售、复制、分发，包括但不限于软件销售、预装、捆绑等，必须获得知启蒙的书面授权和许可；
 * 5、保留权利：本注释未明示授权的其他一切权利仍归知启蒙所有，任何人使用其他权利时必须获得知启蒙的书面同意。
 */
package net.redxyz.repost.resource.presenter;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.annotation.AnIntercept;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.util.Arrays;
import org.zhiqim.orm.dbo.Updater;

import net.redxyz.repost.resource.SessionUser;
import net.redxyz.repost.service.dao.UserDao;
import net.redxyz.top.TaobaoService;
import net.redxyz.top.message.TaobaoResponse;
import net.redxyz.top.message.item.AlibabaItemOperateDownshelfResponse;
import net.redxyz.top.message.item.AlibabaItemOperateUpshelfResponse;
import net.redxyz.top.message.item.ItemSellerGetResponse4_24625;

/**
 * 店铺管理Ajax请求
 */
@AnAlias("ShopPresenter")
@AnIntercept("chkAjax")
public class ShopPresenter
{
    /** 上架处理 */
    public static void doUpRepost(HttpRequest request, long numIid, int num)
    {
        if(num <= 0)
        {
            request.setResponseError("上架宝贝数不能为0，请修改宝贝数后再上架");
            return;
        }
        
        SessionUser user = request.getSessionUser(SessionUser.class);
        String sessionId = user.getSessionId();
        String nick = user.getNick();
        
        AlibabaItemOperateUpshelfResponse resp = TaobaoService.getItemService().itemUpshelf(nick, sessionId, numIid, num);
        if (!resp.isSuccess())
            request.setResponseError(resp.getErrorMsg());
        else
            request.setResponseResult("上架成功");
    }
    
    /** 下架处理 */
    public static void doDownshelf(HttpRequest request, long numIid)
    {
        SessionUser user = request.getSessionUser(SessionUser.class);
        String sessionId = user.getSessionId();
        String nick = user.getNick();
        
        AlibabaItemOperateDownshelfResponse resp = TaobaoService.getItemService().itemDownshelf(nick, sessionId, numIid);
        if (!resp.isSuccess())
            request.setResponseError(resp.getErrorMsg());
        else
            request.setResponseResult("下架成功");
    }
    
    /** 批量上架 */
    public static void doBatchRepost(HttpRequest request, String numIidstr) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        
        int successNum = 0, failureNum = 0;
        long[] numIids = Arrays.toLongArray(numIidstr, ",");
        for (long numIid : numIids)
        {
            TaobaoResponse resp = TaobaoService.getItemService().getItem(sessionUser.getNick(), sessionUser.getSessionId(), numIid);
            if (!resp.isSuccess())
            {
                failureNum++;
                continue;
            }
            
            ItemSellerGetResponse4_24625 response = (ItemSellerGetResponse4_24625)resp;
            int num = response.getItem().getNum();
            if (num <= 0 || !response.getItem().isInstock())
            {
                failureNum++;
                continue;
            }
            
            AlibabaItemOperateUpshelfResponse resp2 = TaobaoService.getItemService().itemUpshelf(sessionUser.getNick(), sessionUser.getSessionId(), numIid, num);
            if (!resp2.isSuccess())
            {
                failureNum++;
                continue;
            }
            
            successNum++;
        }
        
        request.setResponseResult("批处理完成，共["+numIids.length+"]个，成功["+successNum+"]个，失败["+failureNum+"]个");
    }
    
    /** 批量下架 */
    public static String doBatchDownshelf(HttpRequest request, String numIidstr) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        String sessionId = sessionUser.getSessionId();
        String nick = sessionUser.getNick();
        int successNum = 0, failureNum = 0;
        long[] numIids = Arrays.toLongArray(numIidstr, ",");
        for (long numIid : numIids)
        {
            ItemSellerGetResponse4_24625 resp0 = TaobaoService.getItemService().getItem(sessionUser.getNick(), sessionUser.getSessionId(), numIid);
            if (!resp0.isSuccess())
            {
                failureNum++;
                continue;
            }
            
            AlibabaItemOperateDownshelfResponse resp = TaobaoService.getItemService().itemDownshelf(nick, sessionId, numIid);
            if (!resp.isSuccess())
            {
                failureNum++;
                continue;
            }

            successNum++;
        }
        
        return "批处理完成，共["+numIids.length+"]个，成功["+successNum+"]个，失败["+failureNum+"]个";
    }
    
    /** 更新用户手机号码 */
    public static void doUpdateMobile(HttpRequest request, String mobile) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        
        Updater updater = new Updater();
        updater.addMust("nick", sessionUser.getNick());
        updater.addField("mobile", mobile);
        
        UserDao.updateUser(sessionUser.getNick(), updater);
        sessionUser.setMobile(mobile);
        
        request.setResponseResult("更新成功");
    }
    
    /** 更新用户邮箱 */
    public static void doUpdateEmail(HttpRequest request, String email) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        
        Updater updater = new Updater();
        updater.addMust("nick", sessionUser.getNick());
        updater.addField("email", email);
        
        UserDao.updateUser(sessionUser.getNick(), updater);
        sessionUser.setEmail(email);
        
        request.setResponseResult("更新成功");
    }
}
